package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.PYQBean;
import com.xmb.wechat.bean.PYQBean_;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.delegate.PYQDelegate;
import com.xmb.wechat.viewholder.PYQHeaderViewHolder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/14 17:11
 */
public class WechatPYQDetailActivity extends BaseActivity {

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.view)
    View         mView;
    @BindView(R2.id.iv_back1)
    ImageView    mIvBack1;
    @BindView(R2.id.tv_title)
    TextView     mTvTitle;
    @BindView(R2.id.iv_camera1)
    ImageView    mIvCamera1;

    private boolean     isShowUnread;
    private PYQDelegate mDelegate;
    private int         mAvatarMarginTop;

    public WechatPYQDetailActivity() {
        super(R.layout.activity_wechat_pyq_detail);
        isFullScreen = true;
        isChangeStatusBarTxtColor = false;
    }

    public static void start(Activity activity, long pyqBeanId) {
        Intent intent = new Intent(activity, WechatPYQDetailActivity.class);
        intent.putExtra("id", pyqBeanId);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        setNavigationColor(Color.parseColor("#f7f7f7"));

        PYQBean entity = WechatStartApplication.getBoxStore().boxFor(PYQBean.class).query().equal(PYQBean_.id,
                getIntent().getLongExtra("id", 0)).build().findUnique();
        entity = entity == null ? new PYQBean() : entity;//首次进来无任何操作，数据库没数据
        //init view
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                getDistance();
            }
        });
        mDelegate = new PYQDelegate(this, entity == null ? new PYQBean() : entity);
        //head view
        PYQHeaderViewHolder viewHolder = new PYQHeaderViewHolder(this, entity, WechatContactBean.instanceNewContactMe(this, false));
        mDelegate.getAdapter().addHeaderView(viewHolder.headerView);
        //init data
        mDelegate.addData(entity.getTrends());
        isShowUnread = entity.isShowUnreadMsg();
    }

    private void getDistance() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        View firstItem = layoutManager.findViewByPosition(0);
        if (firstItem == null)
            return;
        if (mAvatarMarginTop == 0) {
            int height = firstItem.getHeight();
            mAvatarMarginTop = Double.valueOf((height - ScreenUtils.getScreenWidth() * 0.16) * (isShowUnread ? 0.7 : 0.9)).intValue();
        }
        int top = firstItem.getTop();
        if (Math.abs(top) > mAvatarMarginTop) {
            mView.setVisibility(View.VISIBLE);
            mIvBack1.setVisibility(View.VISIBLE);
            mTvTitle.setVisibility(View.VISIBLE);
            mIvCamera1.setVisibility(View.VISIBLE);
        } else {
            mView.setVisibility(View.GONE);
            mIvBack1.setVisibility(View.GONE);
            mTvTitle.setVisibility(View.GONE);
            mIvCamera1.setVisibility(View.GONE);
        }
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        finish();
    }

}
